/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to design tabs<br>
 * Jquery use for this behavior is <code>$('#id').tabs(options);</code><br>
 * With Wicket, you just need to create a JQTabs Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQTabs extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQTabsPlugin plugin;
	/** JQuery default options */
	private JQTabsOptions options = new JQTabsOptions();

	// TODO
	// tabs( "length" ) Returns: Number
	// Retrieve the number of tabs of the first matched tab pane.

	/**
	 * Contructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQTabs(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQTabsPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQTabsPlugin(key);
		}
		this.plugin = (JQTabsPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Contructor
	 * 
	 * @param id
	 *            wicket component id
	 */
	public JQTabs(String id)
	{
		this(id, null);
	}

	public JQTabsOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQTabsOptions.class))
		{
			this.options = (JQTabsOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQTabs.class, options.getClass(), JQTabsOptions.class);
		}
	}

	public JQTabsPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQTabsPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + " > ul')." + (reset ? "tabs('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return "$('#" + this.getMarkupId() + " > ul')." + options.getDocumentReadyOptions();
	}

	/**
	 * Add a tabs at the specified index with the content loaded from url<br>
	 * Appends a javascript call <code>$('#id').slider('add', url, label, index)</code> to the ajax target.
	 * 
	 * @param url
	 *            url for content load
	 * @param label
	 *            label for tab
	 * @param index
	 *            new tab position
	 * @param target
	 *            Ajax target
	 */
	public void add(String url, String label, int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('add','" + url + "', '" + label + "', " + index + ")");
	}

	/**
	 * Remove the tab at specified index<br>
	 * Appends a javascript call <code>$('#id').tabs('remove', index)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param target
	 *            Ajax target
	 */
	public void remove(int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('remove', " + index + ")");
	}

	/**
	 * Enable the tab at specified index<br>
	 * Appends a javascript call <code>$('#id').tabs('enable', index)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param target
	 *            Ajax target
	 */
	public void enable(int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('enable', " + index + ")");
	}

	/**
	 * Disable the tab at specified index<br>
	 * Appends a javascript call <code>$('#id').tabs('disable', index)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param target
	 *            Ajax target
	 */
	public void disable(int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('disable', " + index + ")");
	}

	/**
	 * Select the tab at specified index<br>
	 * Appends a javascript call <code>$('#id').tabs('select', index)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param target
	 *            Ajax target
	 */
	public void select(int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('select', " + index + ")");
	}

	/**
	 * Load the tab at specified index<br>
	 * Appends a javascript call <code>$('#id').tabs('load', index)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param target
	 *            Ajax target
	 */
	public void load(int index, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('load', " + index + ")");
	}

	/**
	 * Change the url from which an Ajax (remote) tab will be loaded<br>
	 * Appends a javascript call <code>$('#id').tabs('url', index, url)</code> to the ajax target.
	 * 
	 * @param index
	 *            position of the tab
	 * @param url
	 *            url to load
	 * @param target
	 *            Ajax target
	 */
	public void url(int index, String url, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('url', " + index + ", '" + url + "')");
	}

	/**
	 * Destroy tabs interface and revert HTML to the state before creating tabs<br>
	 * Appends a javascript call <code>$('#id').tabs('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('destroy')");
	}

	/**
	 * Set up an automatic rotation through tabs of a tab pane.<br>
	 * Appends a javascript call <code>$('#id').tabs('rotate', ms, continuing)</code> to the ajax target.
	 * 
	 * @param ms
	 *            Amount of time in milliseconds until the next tab in the cycle gets activated. Use 0 or null to stop the rotation
	 * @param continuing
	 *            Whether or not to continue the rotation after a tab has been selected by a user. Default: false.
	 * @param target
	 *            Ajax target
	 */
	public void rotate(int ms, boolean continuing, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').tabs('rotate', " + ms + ", " + continuing + ")");
	}
}
